<?php
use voxfa\log\LogFactory;
require_once 'PHPUnit\Framework\TestCase.php';
require_once 'src/Voxfa.php';

/**
 * test case.
 */
class testLogger extends PHPUnit_Framework_TestCase {
	
	protected function setUp() {
		LogFactory::clear();
		LogFactory::resetConfiguration();
	}
	
	protected function tearDown() {
		LogFactory::clear();
		LogFactory::resetConfiguration();
	}
	
	public function testLoggerExist() {
		$l = LogFactory::getLogger('test');
		self::assertEquals($l->getName(), 'test');
		$l->debug('test');
		self::assertTrue(LogFactory::exists('test'));
	}
	
	public function testCanGetRootLogger() {
		$l = LogFactory::getRootLogger();
		self::assertEquals($l->getName(), 'root');
	}
	
	public function testCanGetASpecificLogger() {
		$l = LogFactory::getLogger('test');
		self::assertEquals($l->getName(), 'test');
	}
	
	public function testCanLogToAllLevels() {
		LogFactory::configure(dirname(__FILE__).'/LoggerTest.properties');
		
		$logger = LogFactory::getLogger('mylogger');
		ob_start();
		$logger->info('this is an info');
		$logger->warn('this is a warning');
		$logger->error('this is an error');
		$logger->debug('this is a debug message');
		$logger->fatal('this is a fatal message');
		
		$v = ob_get_contents();
		ob_end_clean();
		
		$e = 'INFO - this is an info'.PHP_EOL;
		$e .= 'WARN - this is a warning'.PHP_EOL;
		$e .= 'ERROR - this is an error'.PHP_EOL;
		$e .= 'DEBUG - this is a debug message'.PHP_EOL;
		$e .= 'FATAL - this is a fatal message'.PHP_EOL;
		
		self::assertEquals($v, $e);
	}
	
	public function testIsEnabledFor() {
		LogFactory::configure(dirname(__FILE__).'/LoggerTest.properties');
		
		$logger = LogFactory::getLogger('mylogger');
		
		self::assertTrue($logger->isDebugEnabled());
		self::assertTrue($logger->isInfoEnabled());
		
		$logger = LogFactory::getRootLogger();
		
		self::assertFalse($logger->isDebugEnabled());
		self::assertFalse($logger->isInfoEnabled());
	}
	
	public function testGetCurrentLoggers() {
		LogFactory::clear();
		LogFactory::resetConfiguration();
		
		self::assertEquals(0, count(LogFactory::getCurrentLoggers()));
		
		LogFactory::configure(dirname(__FILE__).'/LoggerTest.properties');
		LogFactory::initialize();
		self::assertEquals(1, count(LogFactory::getCurrentLoggers()));
		$list = LogFactory::getCurrentLoggers();
		self::assertEquals('mylogger', $list[0]->getName());
	}
	
	public function testConfigure() {
		LogFactory::resetConfiguration();
		LogFactory::configure();
		self::assertEquals('LoggerConfiguratorBasic', LogFactory::getConfigurationClass());
		self::assertEquals(null, LogFactory::getConfigurationFile());
		
		LogFactory::configure(null, 'MyLoggerClass');
		self::assertEquals('MyLoggerClass', LogFactory::getConfigurationClass());
		self::assertEquals(null, LogFactory::getConfigurationFile());
		
		LogFactory::configure('log4php.xml');
		self::assertEquals('LoggerConfiguratorXml', LogFactory::getConfigurationClass());
		self::assertEquals('log4php.xml', LogFactory::getConfigurationFile());
		
		LogFactory::configure('log4php.xml');
		self::assertEquals('LoggerConfiguratorXml', LogFactory::getConfigurationClass());
		self::assertEquals('log4php.xml', LogFactory::getConfigurationFile());
		
		LogFactory::configure('log4php.properties');
		self::assertEquals('LoggerConfiguratorIni', LogFactory::getConfigurationClass());
		self::assertEquals('log4php.properties', LogFactory::getConfigurationFile());
		
	}

}